layui.use('form', function () {
    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    form.render();
});

/**
 * 手机正则
 * @param phone 传入的参数
 * @returns {boolean}  true 格式正确  false 格式正确
 */
function checkPhone(phone) {
    var phoneReg = /^1[3456789]\d{9}$/;
    return phoneReg.test(phone);
}

/*
* 获取手机验证码的方法
* */
function phoneCode() {
    layui.use("layer", function () {
        let layer = layui.layer;
        layer.msg("已点击，请勿连续点击");
        var phone = $('#userphone').val();
        var phoneReg = /^1[3456789]\d{9}$/;
        if (phone == null || phone === "") {
            $('#error-phone').show();
            return false;
        } else if (!phoneReg.test(phone)) {
            layer.msg("手机号必须为11位数字");
            return false;
        }
        $.post("/login/verification", {"keHuUsePhone": phone}, function (data) {
            if (data.code === 0) {
                $('#phone-code-register').val("     已发送");
                $('#phone-code-register').attr('disabled', true);
                $('#phone-code-register').css('color', 'gray');
            } else if (data.code === 500) {
                layer.msg(data.msg);
            }
        });
    });
}

/*
* 用户手机验证码注册的方法
* */
function phonelRegister() {
    var userphone = $('#userphone').val();
    var password = $('#password').val();
    var repassword = $('#repassword').val();
    var code = $('#userphonecode').val();
    if (password !== repassword) {
        layer.msg("两次输入的密码不同");
        return;
    }

    if (userphone == null | userphone == "") {
        $('#error-phone').show();
        if (repassword === null | repassword === "") {
            $('#error-repassword').show();
            if (code === null | code === "") {
                $('#error-phone-code').show();
                return false;
            } else {
                return false;
            }
        } else {
            if (code === null | code === "") {
                $('#error-phone-code').show();
                return false;
            } else {
                return false;
            }
        }
    } else {
        if (!(checkPhone(userphone))) {
            $('#error-phone-format').show();
            return false;
        }
        if (password === null | password === "") {
            $('#error-phone-password').show();
            if (code === null | code === "") {
                $('#error-phone-code').show();
                return false;
            } else {
                return false;
            }
        } else {
            if (code === null | code === "") {
                $('#error-phone-code').show();
                return false;
            }
        }
    }
    layui.use('layer', function () {
        var layer = layui.layer;
        $.ajax({
            url: '/login/updatepwd',
            type: 'POST',
            data: {"keHuUsePhone": userphone, "keHuUsePassWord": password, "yzm": code},
            dataType: 'json',
            success: function (data) {
                console.log(data)
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 6});
                    window.location.href = "/login.html";
                } else if (data.code === 500) {
                    layer.msg(data.msg, {icon: 5});
                }
            }
        });
    });
}

if (window.parent != window) {
    window.parent.location.reload(true);
}